﻿/* SelTree.js Addon Nice selTree customcontrol */
/* <![CDATA[ */

function clientSideEdit(firstTreeView, secondTreeView, multiLevel,args) 
{
    var destinationNode = args.get_destNode();
    if (destinationNode) 
    {
        firstTreeView.trackChanges();
        secondTreeView.trackChanges();
        var sourceNodes = args.get_sourceNodes();
        var firstNode2 = secondTreeView.get_nodes().getNode(0);
        for (var i = 0; i < sourceNodes.length; i++) 
        {
            var sourceNode = sourceNodes[i];
            sourceNode.get_parent().get_nodes().remove(sourceNode);
            if (destinationNode == firstNode2)
                destinationNode.get_nodes().add(sourceNode);
            else if (multiLevel) 
            {
                if (args.get_dropPosition() == "over") destinationNode.get_nodes().add(sourceNode);
                if (args.get_dropPosition() == "above") insertBefore(destinationNode, sourceNode);
                if (args.get_dropPosition() == "below") insertAfter(destinationNode, sourceNode);
            }
            else 
            {
                if (args.get_dropPosition() == "over") insertAfter(destinationNode, sourceNode);
                if (args.get_dropPosition() == "above") insertBefore(destinationNode, sourceNode);
                if (args.get_dropPosition() == "below") insertAfter(destinationNode, sourceNode);
            }
        }
        destinationNode.set_expanded(true);
        firstTreeView.commitChanges();
        secondTreeView.commitChanges();
    }
}

function insertBefore(destinationNode, sourceNode) 
{
    var destinationParent = destinationNode.get_parent();
    var index = destinationParent.get_nodes().indexOf(destinationNode);
    destinationParent.get_nodes().insert(index, sourceNode);
}

function insertAfter(destinationNode, sourceNode) 
{
    var destinationParent = destinationNode.get_parent();
    var index = destinationParent.get_nodes().indexOf(destinationNode);
    destinationParent.get_nodes().insert(index + 1, sourceNode);
}

/* ]]> */
